﻿using System;
using GpgCore.CityItems;
using GpgCore.Corporations;
using GpgCore.GameWorld;
using GpgCore.MapItems;
using GpgCore.Maps;
using GpgCore.Products;
using Microsoft.Xna.Framework;

namespace GpgCore.Api
{
    public static class MainFactory
    {
        public static void CreateNewGame(string planetName, DateTime startDate, Point mapSize)
        {
            World.NewGame(planetName, startDate, mapSize);
        }

        public static Country CreateCountry(string countryName)
        {
            return new Country(World.Instance.Planet){Name = countryName};
        }

        public static City CreateCity(string cityName, Country country, PlanetMapCell mapCell)
        {
            var city = new City(country, mapCell) {Name = cityName};
            var cells = mapCell.GetNeighborCells();
            foreach (var cell in cells)
            {
                if (cell.BelongsToCity == null)
                    cell.BelongToCity(city);

                var ncells = cell.GetNeighborCells();
                foreach (var ncell in ncells)
                    if (ncell.BelongsToCity == null)
                        ncell.BelongToCity(city);
            }
            return city;
        }

        public static Corporation CreateCorporation(string corpName)
        {
            return new Corporation {Name = corpName};
        }

        public static CityOffice CreateCityOffice(Corporation corporation, City city)
        {
            return new CityOffice(corporation, city);
        }

        public static Farm CreateFarm(PlanetMapCell mapCell, FarmScheme scheme, Corporation corporation)
        {
            if (mapCell.MapItem != null)
                return null; //cell is already occupied

            if (mapCell.BelongsToCity == null)
                return null; //cell is belongs to no city

            if (mapCell.BelongsToCity.GetCityOffice(corporation) == null)
                return null; //city office of this corporation has to be openned in this city
            
            return new Farm(mapCell, scheme, corporation);
        }

        public static Factory CreateFactory(PlanetMapCell mapCell, FactoryScheme scheme, Corporation corporation)
        {
            if (mapCell.MapItem != null)
                return null; //cell is already occupied

            if (mapCell.BelongsToCity == null)
                return null; //cell is belongs to no city

            if (mapCell.BelongsToCity.GetCityOffice(corporation) == null)
                return null; //city office of this corporation has to be openned in this city
            
            return new Factory(mapCell, scheme, corporation);
        }

        public static Mine CreateMine(PlanetMapCell mapCell, MiningScheme scheme, Corporation corporation)
        {
            if (mapCell.MapItem != null)
                return null; //cell is already occupied

            if (mapCell.BelongsToCity == null)
                return null; //cell is belongs to no city

            if (mapCell.BelongsToCity.GetCityOffice(corporation) == null)
                return null; //city office of this corporation has to be openned in this city
            
            return new Mine(mapCell, scheme, corporation);
        }

        public static Shop CreateShop(City city, Corporation corporation, ProductType prodType)
        {
            var cityOffice = city.GetCityOffice(corporation);
            if (cityOffice == null)
                return null; //you have to open city office in city first

            return new Shop(city, prodType, cityOffice);
        }
    }
}
